﻿using System;
using System.Linq;

namespace regxExtensions
{
    public static class Extensions
    {
        /// <summary>
        /// Returns true if the string is equal to the passed string without respect to case
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs">The string to compare against</param>
        /// <returns>Whether the strings match without respect to case</returns>
        internal static bool NoCaseEquals    (this string lhs, string rhs)   {return string.Compare(lhs, rhs, StringComparison.CurrentCultureIgnoreCase) == 0;}
        
        /// <summary>
        /// Tests if the string begins with a given substring, without respect to case
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs">Substring to test for</param>
        /// <returns>True if the string begins with the given substring, regardless of case; false otherwise</returns>
        internal static bool NoCaseStartsWith(this string lhs, string rhs)   {return lhs.StartsWith(rhs, StringComparison.CurrentCultureIgnoreCase);}

        
        /// <summary>
        /// Returns true if the string begins with any of the passed characters
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs">Array of characters to test for</param>
        /// <returns>Whether the string begins with any of the passed-in characters. If called on an empty string, always return false</returns>
        internal static bool BeginsWithAny   (this string lhs, char[] rhs)   {return lhs.IsEmpty() ? false : rhs.Contains(lhs.First());}

        /// <summary>
        /// Returns true if the string is either null or empty
        /// </summary>
        /// <param name="lhs"></param>
        /// <returns>True if the string is null, true if the string is empty, false otherwise</returns>
        internal static bool IsEmpty         (this string lhs)               {return string.IsNullOrEmpty(lhs);}

        /// <summary>
        /// Returns true if the string is not null and is not empty
        /// </summary>
        /// <param name="lhs"></param>
        /// <returns>rue if the string is not null and is not empty</returns>
        internal static bool IsNotEmpty      (this string lhs)               {return !IsEmpty(lhs);}


        /// <summary>
        /// Inserts text into the string, overwriting any characters which are there
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="pos">The 0-based index at which to insert/overwrite the string</param>
        /// <param name="rhs">The string to insert/overwrite</param>
        /// <returns>A copy of the original string with the rhs string replacing characters starting at pos. 
        /// If pos is less than the length of the string, the string is padded with whitespace</returns>
        internal static string OverwriteAt(this string lhs, int pos, string rhs)
        {
            if (pos < 0) pos = 0;
            if (lhs.IsEmpty())     return rhs.PadLeft(rhs.Length + pos);
            if (pos >= lhs.Length) return lhs.PadRight(pos) + rhs;
            
            int rpos = pos + rhs.Length;
            
            string left = lhs.Substring(0, pos);
            string right = (rpos < lhs.Length) ? lhs.Substring(rpos) : string.Empty;
            
            return left + rhs + right;
        }

    }
}
